using AnimCollections;
using System;

namespace DA.AnimGraph {
    public struct Transition {
        public int sourceState;
        public int targetState;
        public float duration;

        public Transition(in TransitionDesc desc) {
            sourceState = desc.sourceState;
            targetState = desc.targetState;
            duration = desc.duration;
        }
    }

    internal struct TransitionInfo : IDisposable {
        public NativeSimpleArray<Transition> transitions;

        public TransitionInfo(in TransitionDesc[] transitionDescs, ref GraphBuffer buffer) {
            if (transitionDescs == null) {
                transitions = default;
                return;
            }

            int length = transitionDescs.Length;
            if (length == 0) {
                transitions = default;
            } else {
                buffer.MallocNativeSimpleArray(out transitions, length);
                for (int i = 0; i < length; i++) {
                    transitions[i] = new(transitionDescs[i]);
                }
            }
        }

        public bool Check(int sourceState, int targetState, out Transition transition) {
            if (transitions.IsCreated) {
                for (int i = 0; i < transitions.Length; i++) {
                    var trans = transitions[i];
                    if (trans.sourceState == sourceState && trans.targetState == targetState) {
                        transition = trans;
                        return true;
                    }
                }
            }

            transition = default;
            return false;
        }

        public void Dispose() {
            if (transitions.IsCreated) {
                transitions.Dispose();
            }
        }
    }
}
